package com.sky.controller.admin;

import com.ejlchina.searcher.SearchResult;
import com.sky.dto.DishDTO;
import com.sky.entity.Dish;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import com.sky.web.controller.BaseController;
import com.sky.web.domain.AjaxResult;
import com.sky.web.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author tzl
 * @Date 2023-07-07 18:26:37  星期五
 * @Description: 菜品管理前端控制器
 */
@Slf4j
@Api(tags = "菜品相关接口")
@RestController
@RequestMapping("/admin/dish")
public class DishController extends BaseController {

    @Autowired
    private DishService dishService;

    /**
     * 菜品分页查询
     * @param dto 查询信息
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("菜品分页查询")
    public TableDataInfo list(DishDTO dto){
        log.info("分页查询：{}", dto);
        SearchResult<DishVO> list = dishService.selectDishList(dto);
        return getDataTable(list);
    }

    /**
     * 菜品查询
     * @param dto 查询信息
     * @return
     */
    @GetMapping("/listAll")
    @ApiOperation("菜品分页查询")
    public AjaxResult listAll(DishDTO dto){
        log.info("条件查询：{}", dto);
        List<DishVO> list = dishService.selectDishListALl(dto);
        return AjaxResult.success(list);
    }

    /**
     * 根据id查询菜品信息
     * @param id 菜品id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询菜品信息")
    public AjaxResult getInfo(@PathVariable Long id){
        return AjaxResult.success(dishService.selectDishById(id));
    }


    /**
     * 新增菜品
     * @param dto 菜品信息
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "dishCache",key = "#dto.categoryId")
    public AjaxResult save(@RequestBody DishDTO dto){
        log.info("新增菜品：{}", dto);
        return toAjax(dishService.insertDish(dto));
    }

    /**
     * 修改菜品状态
     * @param dto 菜品信息
     * @return
     */
    @PutMapping("/updateStatus")
    @ApiOperation("修改菜品状态")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public AjaxResult updateStatus(@RequestBody DishDTO dto){
        return toAjax(dishService.updateDishStatus(dto));
    }

    /**
     * 修改菜品
     * @param dto 菜品信息
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public AjaxResult update(@RequestBody DishDTO dto){
        return toAjax(dishService.updateDish(dto));
    }

    /**
     * 删除菜品
     * @param ids id集合
     * @return
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("删除菜品")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public AjaxResult deleteById(@PathVariable Long[] ids){
        log.info("删除菜品：{}", Arrays.asList(ids));
        return toAjax(dishService.deleteDishByIds(ids));
    }
}
